package com.c.ctgproject.adapter.guang

import android.content.Context
import android.opengl.Visibility
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.CarListEntity
import com.c.ctgproject.ui.collect.*
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import org.jetbrains.anko.startActivity

class CarWeighingAdapter(private val context: Context, private val mList: List<CarListEntity.DataBean.ListBean>, private val name: String, private val mListener: CarWeighingAdapterListener, private val cListener: CarWeighingAdapterCheckedLister, private val mViewModel: CarListViewModel) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_car_weight_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            when (name) {
                "车辆初检" -> {
                    if (mViewModel.searchMine.value == true) {
                        holder.mCheckBox.visibility = View.GONE
                    } else {
                        holder.mCheckBox.visibility = View.VISIBLE
                    }

                }
                "车辆过磅" -> {
                    if (mViewModel.searchMine.value == true) {
                        holder.mCheckBox.visibility = View.GONE
                    } else {
                        holder.mCheckBox.visibility = View.VISIBLE
                    }
                }
            }

            holder.mCheckBox.isChecked = data.checked
            holder.mCheckBox.setOnClickListener {
                mList[position].checked = holder.mCheckBox.isChecked
                cListener.onWeightChecked(position, holder.mCheckBox.isChecked)
                notifyItemChanged(position)
            }

            holder.mBTN.setOnClickListener {
                when (name) {

                    "车辆过磅" -> {
                        val carIdList=ArrayList<Int>()
                        carIdList.add(data.carId)
                        context.startActivity<CarWeighingChildActivity>(
                            MyConstants.POSITION to position,
                            MyConstants.CAR_ID to data.carId,
                            MyConstants.CAR_ID_LIST to Gson().toJson(carIdList)
                        )
                    }
                    "车辆初检" -> context.startActivity<CarFirstCheckActivity>(
                        MyConstants.POSITION to position,
                        MyConstants.CAR_ID to data.carId,
                    )
                    "登记入库" -> {
                        context.startActivity<CarInWarehouseActivity>(
                            MyConstants.CAR_ID to data.carId,
                            MyConstants.POSITION to position,
                            MyConstants.DATA to getGson().toJson(data),
                            MyConstants.IS_BOHUI to (data.entrance == 2),
                            MyConstants.UPDATE to (holder.mBTN.text == "编辑")
                        )
                    }
                    else -> {
                         val carIdList=ArrayList<Int>()
                        carIdList.add(data.carId)

                        context.startActivity<CarWeighingChildActivity>(MyConstants.CAR_ID to data.carId,MyConstants.CAR_ID_LIST to Gson().toJson(carIdList))
                    }
                }
            }
            holder.mUserNameTV.text = data.createPersonName.noDataHandler()
            holder.mVinTV.text = data.carVin.noDataHandler()
            holder.mCarCodeTV.text = data.carCode.noDataHandler()
            holder.mCarFromTV.text = data.carSource.noDataHandler()
            holder.carPlate.text = data.carPlate.noDataHandler()

            val carType = when (data.carType) {
                1 -> "社会车"
                2 -> "试验车"
                3 -> "外包车"
                4 -> "五类车"
                5 -> "农机具"
                else -> "暂无数据"
            }
            holder.mCarModelTV.text = carType
            val carStatus = when (data.carStatus) {
                1 -> "整车"
                2 -> "车架"
                else -> "暂无数据"
            }
            holder.mCarStatueTV.text = carStatus
            holder.mTimeTV.text = data.createTime.noDataHandler()
            holder.mEnergyTypeLL.visibility=if(mViewModel.searchMine.value==true)View.VISIBLE else View.GONE
            holder.mEnergyTypeTV.text=when(data.energy){
                1->"燃油车"
                2->"电动车"
                3->"油电混合"
                else->"暂无数据"
            }
            holder.mParentLL.setOnClickListener {
                //车辆详情
//                context.startActivity<CarInfoActivity>(MyConstants.CAR_ID to data.carId)
                context.startActivity<SearchCarInfoActivity>(MyConstants.CAR_ID to data.carId)
            }

            //按钮文字
            holder.mBTN.text = when (name.handlerNull()) {
                "登记入库" -> "入库"
                "车辆过磅" -> "过磅"
                "车辆初检" -> "初检"
                else -> name.handlerNull()
            }

            holder.mBottomRL.setOnClickListener {
                holder.mBTN.performClick()
            }
            if (name == "车辆预处理") {
                holder.mHandlerBottomRL.visibility = View.VISIBLE
                holder.mBottomRL.visibility = View.GONE
            } else {
                holder.mHandlerBottomRL.visibility = View.GONE
                holder.mBottomRL.visibility = View.VISIBLE
            }

            holder.mHandlerBatterRL.visibility = if (data.batteryDisassemble == 0) View.VISIBLE else View.GONE
            holder.mHandlerWaterRL.visibility = if (data.oilWaterDrainage == 0) View.VISIBLE else View.GONE


            //拆电池(预处理)
            holder.mHandlerBatterRL.setOnClickListener {
                holder.mHandlerBatterBTN.performClick()
            }
            //拆电池
            holder.mHandlerBatterBTN.setOnClickListener {
                context.startActivity<HandlerBatteryV2Activity>(
                    MyConstants.POSITION to position,
                    MyConstants.CAR_ID to data.carId
                )
            }
            //排油水(预处理)
            holder.mHandlerWaterRL.setOnClickListener {
                holder.mHandlerWaterBTN.performClick()
            }
            holder.mHandlerWaterBTN.setOnClickListener {
                mListener.onWaterClick(position)
            }
            //是否是驳回的
            if (name == "登记入库" && data.entrance == 2) {
                holder.mStatueTV.visibility = View.VISIBLE
            } else if (name == "车辆初检" && data.entrance == 2&&mViewModel.searchMine.value==true){
                    holder.mStatueTV.visibility = View.VISIBLE
            }else {
                holder.mStatueTV.visibility = View.GONE
            }


            //处理入库按钮
            //登记入库状态 1-已完成 0-未完成 2-驳回 //新增情况就是如果后台返回参数 isEditable  1:可以编辑 0:不可编辑
            if (name == "登记入库")
                if (data.entrance == 1) {//||data.entrance==2
                    if (data.isEditable == 1) {
                        holder.mBTN.visibility = View.VISIBLE
                        holder.mBTN.text = "编辑"
                    } else {
                        holder.mBTN.visibility = View.GONE
                    }
                } else {
                    holder.mBTN.text = "入库"
                    holder.mBTN.visibility = View.VISIBLE
                }


            //
            //过磅状态 1-已完成 0-未完成
            //if (name == "车辆过磅") holder.mBTN.visibility = if (data.pound == 1) View.GONE else View.VISIBLE
            if (name == "车辆过磅") {
                if(mViewModel.searchMine.value == true){
                    holder.mBTN.visibility = View.VISIBLE
                    holder.mBTN.text="编辑"
                }else{
                    holder.mBTN.visibility = View.GONE
                }

            }
            //if (name == "车辆初检") holder.mBTN.visibility = if (data.carCheck == 1) View.GONE else View.VISIBLE
            if (name == "车辆初检") {
                holder.mBTN.text = if (mViewModel.searchMine.value == true) "驳回" else "初检"
                holder.mBTN.background = if (mViewModel.searchMine.value == true) context.getDrawable(R.drawable.bg_btn_red_radius) else context.getDrawable(R.drawable.bg_btn_green_radius)
                if(data.entrance==2){
                    holder.mBTN.visibility=View.GONE
                }else{
                    holder.mBTN.visibility=View.VISIBLE
                }
            }
            if (name == "车辆预处理") holder.mHandlerBottomRL.visibility = if (data.pretreatment == 1) View.GONE else View.VISIBLE
        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val carPlate: TextView = view.findViewById(R.id.carPlate)
        val mStatueTV: TextView = view.findViewById(R.id.mStatueTV)
        val mCarCodeTV: TextView = view.findViewById(R.id.mCarCodeTV)
        val mCarFromTV: TextView = view.findViewById(R.id.mCarFromTV)
        val mUserNameTV: TextView = view.findViewById(R.id.mUserNameTV)
        val mVinTV: TextView = view.findViewById(R.id.mVinTV)
        val mCarModelTV: TextView = view.findViewById(R.id.mCarModelTV)
        val mCarStatueTV: TextView = view.findViewById(R.id.mCarStatueTV)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mBTN: Button = view.findViewById(R.id.mBTN)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
        val mBottomRL: RelativeLayout = view.findViewById(R.id.mBottomRL)
        val mHandlerBottomRL: RelativeLayout = view.findViewById(R.id.mHandlerBottomRL)
        val mHandlerWaterBTN: Button = view.findViewById(R.id.mHandlerWaterBTN)
        val mHandlerBatterBTN: Button = view.findViewById(R.id.mHandlerBatterBTN)
        val mHandlerWaterRL: RelativeLayout = view.findViewById(R.id.mHandlerWaterRL)
        val mHandlerBatterRL: RelativeLayout = view.findViewById(R.id.mHandlerBatterRL)
        val mCheckBox: CheckBox = view.findViewById(R.id.mCheckBox)
        val mEnergyTypeTV:TextView=view.findViewById(R.id.mEnergyTypeTV)
        val mEnergyTypeLL:LinearLayoutCompat=view.findViewById(R.id.mEnergyTypeLL)
    }

    interface CarWeighingAdapterListener {
        fun onWaterClick(position: Int)
    }

    interface CarWeighingAdapterCheckedLister {
        fun onWeightChecked(position: Int, checked: Boolean)
    }
}